Kumar Sangakkara and skipper Angelo Mathews struck brisk half-centuries to help Sri Lanka set up a thrilling finale to the series-deciding second Test against South Africa on Sunday.
Sangakkara (72) hit his tenth 50-plus score of the year and Mathews chipped in with an unbeaten 63 as the hosts declared their second innings on 229-8 shortly before close of play to set the visitors an imposing target of 369.
At stumps on a rain-interrupted fourth day, South Africa were 38-1, needing 331 more runs off a minimum 98 overs to pull off a series-clinching win.
Quinton de Kock (21) and Dean Elgar (13) were the not out batsmen at the crease.
The hosts, who posted 421 in their first essay, had bowled out South Africa for 282 in their first innings to gain a 139-run lead.
South Africa lead the two-Test series 1-0 after winning the opener in Galle by 153 runs. Even a draw in this game will see the Proteas reclaim the world number one ranking from Australia.